package com.yunbao.hujin.zqqgetuidemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import com.igexin.sdk.PushManager;
import com.yunbao.hujin.zqqgetuidemo.framenttoactivity.CallBack;
import com.yunbao.hujin.zqqgetuidemo.service.DemoIntentService;
import com.yunbao.hujin.zqqgetuidemo.service.MyGetuiService;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements CallBack {

    public static final String MY_BROADCAST = "my_broadcast";

    private static TextView textView;//接受fragment

    private static DemoHandler handler;
//    private LocalBroadcastManager localBroadcastManager;
    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 禁止截屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        setContentView(R.layout.activity_main);

        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        // MyGetuiService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), MyGetuiService.class);

        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);

        textView = findViewById(R.id.tv_main);
        handler = new DemoHandler();

        registerLocalBroadcast();//注册 获取经纬度的广播
    }


    /*********---------第一种方式--Handler------********************/
    public static void sendMessage(Message message) {
        handler.sendMessage(message);
    }

    public static class DemoHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
//                    textView.append(msg.obj + "\n");
                    textView.setText(msg.obj + "\n");
                    Log.e("Tag--MainActivity:", msg.obj.toString());
                    break;

                case 1:
                    textView.setText((String) msg.obj);
                    break;
            }
        }
    }

    /*********---------第二种方式--BroadCastReciver------********************/
    private void registerLocalBroadcast() {
//        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        myBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MY_BROADCAST);
//        localBroadcastManager.registerReceiver(myBroadcastReceiver, filter);
        registerReceiver(myBroadcastReceiver, filter);
    }

    class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String data = intent.getStringExtra("data");
            Log.e("Tag-Reciver", data);
            if (data != null) {
//                try {
//                    JSONObject jsonObject = new JSONObject(data);
//                    jsonObject.get("name").toString();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
                textView.setText(data);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBroadcastReceiver != null) {
            unregisterReceiver(myBroadcastReceiver);
        }
    }


    /********************-------End-------************************/

    /**
     * 为了接受来自Fragment的数据
     */
    @Override
    public void putStringValue(String s) {
        textView.setText(s);
    }



}
